package demo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zjq
 * @version 创建时间：2022-4-12 16:09:35
 */
public class random {

    /**
     * main方法描述: 随机数工具
     *
     * @param args
     * @author : Ricky
     * @createTime : Jun 19, 2015 1:16:54 PM
     */
    public static void main(String[] args) {
//		for (int k = 0; k < 100000; k++) {
//			int[] sort = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
//			int[] newss = new random().randomSort(sort);
//			for (int p = 0; p < sort.length; p++) {
//				if (sort[p] == newss[p]) {
//					System.out.println("相等了");
//				}
//			}
//			if (k % 100 == 0) {
//				System.out.println(k);
//				for (int p = 0; p < sort.length; p++) {
//					System.out.print(newss[p] + " ");
//				}
//			}
//		}

        for (int k = 0; k < 1; k++) {
            int[] sort = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
            int[] newss = new random().randomSort(sort);
            List<Integer> list = Arrays.stream(newss).boxed().collect(Collectors.toList());
            System.out.println(list);
        }
    }

    /**
     * randomSort方法描述: 将一个数组进行随机排序，使每个序列位置上的数字都不是原来的数字 使用场景：****。
     *
     * @param sort 需要打乱顺序的数组
     * @return
     * @author : Ricky
     * @createTime : Jun 19, 2015 3:38:55 PM
     */
    public int[] randomSort(int[] sort) {
        List<Integer> sList = new ArrayList<Integer>();
        List<Integer> rList = new ArrayList<Integer>();
        int[] rSort = new int[sort.length];
        for (int s : sort) {
            sList.add(s);
        }
        int count = 0;
        Integer lastValue = null;
        while (sList.size() > 0) {
            int randSeq = getRandom(sList.size());
            if (sList.get(randSeq) != sort[count]) {
                rList.add(sList.get(randSeq));
                sList.remove(randSeq);
                count++;
            } else {
                if (count + 1 == sort.length) {
                    if (sList.get(randSeq) == sort[count]) {
                        lastValue = sort[count];
                        rList.add(sList.get(randSeq));
                        sList.remove(randSeq);
                    }
                    break;
                }
            }
        }
        for (int i = 0; i < rList.size(); i++) {
            rSort[i] = rList.get(i);
        }
        if (lastValue != null) {
            int t = rSort[sort.length - 1];
            int randomSeq = getRandom(sort.length - 1);
            rSort[sort.length - 1] = rSort[randomSeq];
            rSort[randomSeq] = t;
        }
        return rSort;
    }

    /**
     * getRandom方法描述: 获得小于quan的一个整数
     *
     * @param quan 产生的整形数字必须比它小
     * @return
     * @author : Ricky
     * @createTime : Jun 19, 2015 1:44:19 PM
     */
    private int getRandom(int quan) {
        int newInt = quan;
        while (!(newInt < quan)) {
            String randomStr = "" + quan * Math.random();
            if (randomStr.contains("."))
                randomStr = randomStr.substring(0, randomStr.indexOf("."));
            newInt = Integer.parseInt(randomStr);
        }
        return newInt;
    }
}
